﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IObjects;
using Entities;
using DataObjects.EFData;
using Utilities;

namespace DataObjects
{
    public class Feedback : IFeedback
    {
        private FeedBackInfo _feedBackInfo;
        private string mode = string.Empty;
        private WebOnlineEntities datacontext;
        # region implement interface
        public FeedBackInfo FeedBackInfo
        {
            get
            {
                return _feedBackInfo;
            }
            set
            {
                _feedBackInfo = value;
            }
        }

        public string Mode
        {
            get
            {
                return mode;
            }
            set
            {
                mode = value;
            }
        }

        public int Save()
        {
            if (mode == Constants.MODE_INSERT)
            {
                return Insert();
            }
            if (mode == Constants.MODE_UPDATE)
            {
                return Update();
            }
            if (mode == Constants.MODE_INSERT)
            {
                return Delete();
            }
            throw new Exception("Mode is invalid in Feedback object");
        }

        

        public IList<FeedBackInfo> GetFeedbacks()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var feedbackList = datacontext.TB_FeedBack.Select(c => c.ToFeedBackInfo()).ToList();
                return feedbackList;
            }
        }

        public FeedBackInfo GetById(int id)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var feedback = datacontext.TB_FeedBack.Where(c => c.FeedBackId==id).FirstOrDefault();
                if (feedback != null)
                {
                    return feedback.ToFeedBackInfo();
                }
                return null;
            }
        }
        #endregion
        #region methods
        private int Delete()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.DeleteObject(datacontext.TB_FeedBack.FirstOrDefault(c => c.FeedBackId == _feedBackInfo.Id));
                return datacontext.SaveChanges();
            }
        }

        private int Update()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var feedback = datacontext.TB_FeedBack.Where(c => c.FeedBackId == _feedBackInfo.Id).FirstOrDefault();
                feedback.Content = _feedBackInfo.Content;
                feedback.Email = _feedBackInfo.Email;
                feedback.IsNew = _feedBackInfo.IsNew;
 
                return datacontext.SaveChanges();
            }
        }

        private int Insert()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.AddToTB_FeedBack(this._feedBackInfo.ToTB_FeedBack());
                return datacontext.SaveChanges();
            }
        }
        #endregion
    }
}
